<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_content_info extends content_info{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($info_id='') {
        global $_G;
		$detail = array();
        $info_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
		$detail = $this->one_info('content_info','info_id',$info_id);
        //DEBUG 调试输出
        if($_G['gp_api']){
            $return['code'] = '0';
            $detail_format['id']=$detail['info_id'];
            $detail_format['time']=date('Y-m-d H:i',$detail['create_dateline']);
            $detail_format['create_dateline']=$detail['create_dateline'];
            $detail_format['title']=$detail['title'];
            $detail_format['content']=trim($detail['content']);
            $detail_format['nav']=true;
            $return['data']=$detail_format;
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            include template('content/info/detail'); 
        }
	}
    
	/*
	*  添加系统信息
	*/
	public function do_add() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$content_info = isset($_REQUEST['content_info']) && !empty($_REQUEST['content_info']) ? $_REQUEST['content_info']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach($content_info AS $key => $value){
                if(is_array($value) && !empty($value)){
                    $content_info[$key] = implode(",", $value);
                }
            }
            $content_info['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('content_info',$content_info);
            if($insert_id){
                showmessage('操作成功','index.php?mod=content&action=info&do=index');
            }else{
                showmessage('操作失败','index.php?mod=content&action=info&do=index');
            }
		}else{
            //DEBUG 获取信息分类开始
            $page_result = array();
            $content_cate_module_class_path = libfile('content/cate', 'class','..','/admin');
            if(file_exists($content_cate_module_class_path)){
                require_once $content_cate_module_class_path;
                $content_cate_module = new content_cate();
                $page_condition=array(
                    'page' => 1,//int 请求页面 页码
                    'limit' => 100,//int 每页请求个数
                    'perpage' => 100,//int 每页显示个数
                    'wheresql' => '',//string //条件SQL语句 
                    'orderby' => '',//string 排序规则
                );
                $page_result = $this->index('content_cate',$page_condition);
            }
            //DEBUG 获取信息分类结束
			include template('content/info/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$content_info = isset($_REQUEST['content_info']) && !empty($_REQUEST['content_info']) ? $_REQUEST['content_info']:'';
        $info_id = isset($_REQUEST['info_id']) ? $_REQUEST['info_id'] : '';
        if($info_id){
            if('1'==$issubmit){
                $content_info['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($content_info AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $content_info[$key] = implode(",", $value);
                    }
                }                
                $where = array('info_id'=>$info_id);
                $effect_row = $this->edit('content_info',$content_info,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=content&action=info&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=content&action=info&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('content_info','info_id',$info_id);
                //DEBUG 获取信息分类开始
                if(!empty($info['info_cateid'])){
                    $result = $this->one_info('content_cate', 'info_cateid', $info['info_cateid']);
                    $info['info_cateid_name']=$result['title'];
                }
                //DEBUG 获取信息分类结束
                include template('content/info/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=content&action=info&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function do_delete() {
        global $_G;
        $info_id = isset($_REQUEST['info_id']) ? $_REQUEST['info_id'] : '';
        if($info_id){
            $effect_row = $this->delete("content_info", array("info_id" => $info_id), $limit=1);
            if($effect_row){
                showmessage('操作成功','index.php?mod=content&action=info&do=index');
            }else{
                showmessage('操作失败','index.php?mod=content&action=info&do=index');
            }
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND title LIKE '%".$keyword."%' ";
        }
        $so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        $wheresql .= " AND so_id = ".$so_id;
        $info_cateid = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if($info_cateid){
            $wheresql .= " AND info_cateid = '".$info_cateid."' ";
        }
        $orderby = " ORDER BY info_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('content_info',$page_condition);
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $info_cateid_array = array();
        foreach($page_result['page_data'] AS $key => $value){
            $tmp = array();
            $tmp['id']=$value['info_id'];
            $tmp['time']=date('Y-m-d H:i',$value['create_dateline']);
            $tmp['create_dateline']=$value['create_dateline'];
            $tmp['title']=$value['title'];
            $info_cateid_array[]=$tmp;
        }
        //$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
        //DEBUG 获取各类信息分类名称 结束
        $page_result['page_data'] = $info_cateid_array;
        //DEBUG 返回输出
        $api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
        $client=isset($_REQUEST['client']) ? $_REQUEST['client']:'wap';
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data']=$page_result;
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=".$keyword);
            include template('famous/teachers/index');   
        }
	}
    
    /*
	*  关于我们左侧菜单
	*/
	public function do_about(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 50;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $info_cateid = 1003;
        if($info_cateid){
            $wheresql .= " AND info_cateid = '".$info_cateid."' ";
        }
        $so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        $wheresql .= " AND so_id = ".$so_id;
        $orderby = " ORDER BY info_id ASC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('content_info',$page_condition);
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $info_cateid_array = array();
        foreach($page_result['page_data'] AS $key => $value){
            $tmp = array();
            $tmp['id']=$value['info_id'];
            $tmp['title']=$value['title'];
            $tmp['create_dateline']=$value['create_dateline'];
            $info_cateid_array['list'][]=$tmp;
            if($value['info_id']==4){
                $info_cateid_array['content']=$value['content'];
            }
        }
        $id=isset($_REQUEST['id']) ? $_REQUEST['id']:'';
        if($id){
            $detail_content_info = $this->one_info('content_info','info_id',$id);
            $info_cateid_array['content']=$detail_content_info['content'];
        }
        //$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
        //DEBUG 获取各类信息分类名称 结束
        $page_result['page_data'] = $info_cateid_array;
        //DEBUG 返回输出
        $api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
        $client=isset($_REQUEST['client']) ? $_REQUEST['client']:'wap';
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data']=$page_result['page_data'];
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=".$keyword);
            include template('famous/teachers/index');   
        }
	}
    
    /*
	*  帮助中心左侧菜单
	*/
	public function do_help(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 80;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $info_cateid = "1001,1002";
        if($info_cateid){
            $wheresql .= " AND info_cateid IN (".$info_cateid.") ";
        }
        $so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        $wheresql .= " AND so_id = ".$so_id;
        $orderby = " ORDER BY info_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('content_info',$page_condition);
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $info_cateid_array = array();
        $info_cateid_array['list'][] = array('id'=>'1001','title'=>'新手指南','level'=>'1');
        foreach($page_result['page_data'] AS $key => $value){
            if($value['info_cateid']==1001){
                $tmp = array();
                $tmp['id']=$value['info_id'];
                $tmp['title']=$value['title'];
                $tmp['create_dateline']=$value['create_dateline'];
                $tmp['level']="2";
                if($value['info_id']==8){
                    $info_cateid_array['content']=$value['content'];
                }
                $info_cateid_array['list'][]=$tmp;   
            }
        }
        $info_cateid_array['list'][] = array('id'=>'1002','title'=>'常见问题','level'=>'1');
        foreach($page_result['page_data'] AS $key => $value){
            if($value['info_cateid']==1002){
                $tmp = array();
                $tmp['id']=$value['info_id'];
                $tmp['title']=$value['title'];
                $tmp['level']="2";
                if($value['info_id']==8){
                    $info_cateid_array['content']=$value['content'];
                }
                $info_cateid_array['list'][]=$tmp;   
            }
        }
        $id=isset($_REQUEST['id']) ? $_REQUEST['id']:'';
        if($id){
            $detail_content_info = $this->one_info('content_info','info_id',$id);
            $info_cateid_array['content']=$detail_content_info['content'];
        }
        //$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
        //DEBUG 获取各类信息分类名称 结束
        $page_result['page_data'] = $info_cateid_array;
        //DEBUG 返回输出
        $api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
        $client=isset($_REQUEST['client']) ? $_REQUEST['client']:'wap';
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data']=$page_result['page_data'];
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=".$keyword);
            include template('famous/teachers/index');   
        }
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY info_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('content_info',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
}
?>